package com.probox.common.entity.service.platfom.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.PublishStatusEnum;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.exception.PlatformException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.platfom.PlatformInfo;
import com.probox.common.entity.domain.platfom.PlatformInfoTemplate;
import com.probox.common.entity.domain.platfom.PlatformPageConfig;
import com.probox.common.entity.domain.platfom.PlatformTemplate;
import com.probox.common.entity.domain.platfom.vo.PlatformTemplateVo;
import com.probox.common.entity.domain.platfom.vo.UseTypeVo;
import com.probox.common.entity.domain.platfom.vo.api.PlatformTemplateView;
import com.probox.common.entity.mapper.platfom.PlatformTemplateMapper;
import com.probox.common.entity.service.platfom.IPlatformInfoService;
import com.probox.common.entity.service.platfom.IPlatformInfoTemplateService;
import com.probox.common.entity.service.platfom.IPlatformPageConfigService;
import com.probox.common.entity.service.platfom.IPlatformTemplateService;
import com.probox.common.redis.service.RedisService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 平台模板Service业务层处理
 *
 * @date 2021-06-10
 */
@Service
@Transactional
public class PlatformTemplateServiceImpl extends ServiceImpl<PlatformTemplateMapper, PlatformTemplate> implements IPlatformTemplateService {
    @Autowired
    private PlatformTemplateMapper platformTemplateMapper;

    @Autowired
    private IPlatformPageConfigService platformPageConfigService;

    @Autowired
    private IPlatformInfoTemplateService platformInfoTemplateService;

    @Autowired
    private IPlatformInfoService iPlatformInfoService;

    @Autowired
    private RedisService redisService;

    @Override
    public List<PlatformTemplate> getTemplatePage(Long platformUid) {
        return platformTemplateMapper.getTemplatePage(platformUid);
    }

    @Override
    public List<PlatformTemplate> getTemplatePageStatus(Long platformUid) {
        return platformTemplateMapper.getTemplatePageStatus(platformUid);
    }

    @Override
    public String getOpenScreen() {
        String str = redisService.get(CachePrefix.OPEN_SCREEN, String.class);
        if (ObjectUtil.isEmpty(str)) {
            QueryWrapper<PlatformTemplate> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
            queryWrapper.eq("publish_status", DataStatus.valid.getKey());
            PlatformTemplate platformTemplate = platformTemplateMapper.selectOne(queryWrapper);
            if (ObjectUtil.isNotEmpty(platformTemplate)) {
                if (ObjectUtil.isNotEmpty(platformTemplate.getOpenScreen())) {
                    str = platformTemplate.getOpenScreen();
                    redisService.set(CachePrefix.OPEN_SCREEN, platformTemplate.getOpenScreen(), 5L, TimeUnit.MINUTES);
                }
            }
        }
        return str;
    }

    @Override
    public boolean saveOpenScreen(PlatformTemplate template) {
        PlatformTemplate platformTemplate = new PlatformTemplate();
        platformTemplate.setTemplateId(template.getTemplateId());
        platformTemplate.setOpenScreen(template.getOpenScreen());
        boolean b = this.updateById(platformTemplate);
        redisService.set(CachePrefix.OPEN_SCREEN, platformTemplate.getOpenScreen(), 5L, TimeUnit.MINUTES);
        return b;
    }

    /**
     * 查询平台模板
     *
     * @param templateId 平台模板ID
     * @return 平台模板
     */
    @Override
    public PlatformTemplate selectPlatformTemplateById(Long templateId) {
        return this.getById(templateId);
    }

    @Override
    public PlatformTemplate selectPlatformTemplate(Long templateUid) {
        QueryWrapper<PlatformTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TEMPLATE_ID", templateUid);
        queryWrapper.eq("PUBLISH_STATUS", DataStatus.valid.getKey());
        return platformTemplateMapper.selectOne(queryWrapper);
    }


    /**
     * 查询平台模板列表
     *
     * @param platformTemplate 平台模板
     * @return 平台模板
     */
    @Override
    public List<PlatformTemplateVo> queryList(PlatformTemplate platformTemplate, Long[] uids) {
        List<PlatformTemplateVo> list = new ArrayList<>();
        QueryWrapper<PlatformTemplate> queryWrapper = Wrappers.query();
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(platformTemplate.getTradeCode())) && platformTemplate.getTradeCode() != null) {
            queryWrapper.eq("trade_code", platformTemplate.getTradeCode());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformTemplate.getTemplateName())) && platformTemplate.getTemplateName() != null) {
            queryWrapper.eq("template_name", platformTemplate.getTemplateName());
        }
        if (ObjectUtil.isNotEmpty(platformTemplate.getPublishStatus())) {
            queryWrapper.eq("PUBLISH_STATUS", platformTemplate.getPublishStatus());
        }
        queryWrapper.in("TEMPLATE_ID", Arrays.asList(uids));
        queryWrapper.orderByDesc("update_time");
        List<PlatformTemplate> queryList = platformTemplateMapper.selectList(queryWrapper);
        if (ObjectUtil.isEmpty(queryList)) return list;
        queryList.forEach(temp -> {
            PlatformTemplateVo vo = new PlatformTemplateVo();
            List<String> arr = new ArrayList<>();
            BeanUtils.copyProperties(temp, vo);
            if (ObjectUtil.isNotEmpty(temp.getHomePageImage())) arr.add(temp.getHomePageImage());
            if (ObjectUtil.isNotEmpty(temp.getClassPageImage())) arr.add(temp.getClassPageImage());
            if (ObjectUtil.isNotEmpty(temp.getShopCartImage())) arr.add(temp.getShopCartImage());
            if (ObjectUtil.isNotEmpty(temp.getPersonCenterImage())) arr.add(temp.getPersonCenterImage());
            if (ObjectUtil.isNotEmpty(temp.getShopDetailImage())) arr.add(temp.getShopDetailImage());
            vo.setImgUrl(arr.toArray(new String[0]));
            list.add(vo);
        });
        return list;
    }

    /**
     * 查询平台模板列表
     *
     * @param platformTemplate 平台模板
     * @return 平台模板
     */
    @Override
    public List<PlatformTemplateVo> queryList(PlatformTemplate platformTemplate, int ind) {
        List<PlatformTemplateVo> list = new ArrayList<>();
        PlatformInfoTemplate platformInfoTemplate = new PlatformInfoTemplate();
        platformInfoTemplate.setDataStatus(DataStatus.valid.getKey());
        if (ind == 0) {
            if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
                platformTemplate.setPublishStatus(PublishStatusEnum.publish.getKey());
            }
            platformInfoTemplate.setPlatformUid(1L);
        } else {
            platformInfoTemplate.setPlatformUid(SecurityUtils.getPlatformId());
            platformTemplate.setPublishStatus(PublishStatusEnum.draft.getKey());
        }

        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateService.queryListSs(platformInfoTemplate);
        if (ObjectUtil.isNotEmpty(platformInfoTemplates)) {
            Long[] uids = new Long[platformInfoTemplates.size()];
            AtomicReference<Integer> index = new AtomicReference<>(0);
            platformInfoTemplates.forEach(vo -> {
                uids[index.get()] = vo.getTemplateUid();
                index.getAndSet(index.get() + 1);
            });
            return queryList(platformTemplate, uids);
        }
        return list;
    }

    @Override
    public Integer queryListCount(PlatformTemplate platformTemplate) {
        PlatformInfoTemplate platformInfoTemplate = new PlatformInfoTemplate();
        platformInfoTemplate.setDataStatus(DataStatus.valid.getKey());
        if (ObjectUtil.isNotEmpty(SecurityUtils.getCustomerPlatformUid())) {
            platformInfoTemplate.setPlatformUid(SecurityUtils.getCustomerPlatformUid());
        } else {
            platformInfoTemplate.setPlatformUid(SecurityUtils.getPlatformId());
        }
        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateService.queryList(platformInfoTemplate);
        if (ObjectUtil.isNotEmpty(platformInfoTemplates)) {
            Long[] uids = new Long[platformInfoTemplates.size()];
            AtomicReference<Integer> index = new AtomicReference<>(0);
            platformInfoTemplates.forEach(vo -> {
                uids[index.get()] = vo.getTemplateUid();
                index.getAndSet(index.get() + 1);
            });
            return queryList(platformTemplate, uids).size();
        }
        return 0;
    }

    @Override
    public boolean deletePlatformTemplateByIds(Long templateUid) {
        UpdateWrapper<PlatformTemplate> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("TEMPLATE_ID", templateUid);
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("PUBLISH_STATUS", PublishStatusEnum.revisions.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        this.update(updateWrapper);
        platformPageConfigService.removeComponent(templateUid, PublishStatusEnum.revisions.getKey(), null, null, null);
        platformInfoTemplateService.deletePlatformInfoTemplateByIds(SecurityUtils.getPlatformId(), templateUid);
        return true;
    }

    /**
     * 新增平台模板下的页面组件数据
     *
     * @param platformTemplateView 平台模板
     * @return 结果
     */
    @Override
    public Long insertPlatformTemplate(PlatformTemplateView platformTemplateView) {
        Long templateUid = 0L;
        if (ObjectUtil.isEmpty(platformTemplateView)) throw new PlatformException("数据有错误,请刷新页面！");
        if (PublishStatusEnum.publish.getKey().equals(platformTemplateView.getPublishStatus())) {
            PlatformInfo platformInfo = iPlatformInfoService.selectPlatformInfoById();
            if (DateUtil.date().compareTo(platformInfo.getAuthTime()) > 0)
                throw new BaseException("当前店铺使用时间已结束");
        }
        //数据校验
        platformTemplateView.setPlatformUid(SecurityUtils.getPlatformId());
        platformTemplateView.setUserId(SecurityUtils.getUserId());
        platformTemplateView.setUserName(SecurityUtils.getUsername());
        platformTemplateView.setTime(DateUtil.date());
        PlatformInfoTemplate infoTemplate = new PlatformInfoTemplate();
        infoTemplate.setPlatformUid(SecurityUtils.getPlatformId());
        Long userId = SecurityUtils.getUserId();// 只有超级管理员才能添加模板
        platformTemplateView.setIsAdmin(SecurityUtils.isAdmin(userId));
        if (platformTemplateView.getIsAdmin()) {// 管理员
            if (ObjectUtil.isEmpty(platformTemplateView.getTemplateId())) {
                platformTemplateView.setSourceTemplateId(0l);
                PlatformTemplate platformTemplate = saveTemplate(platformTemplateView);// 添加模板信息
                platformTemplateView.setTemplateId(platformTemplate.getTemplateId());
                platformPageConfigService.insertPlatformPageConfig(platformTemplateView);// 添加模板页面信息
                infoTemplate.setTemplateUid(platformTemplate.getTemplateId());
                platformInfoTemplateService.insertPlatformInfoTemplate(infoTemplate, platformTemplateView);
                templateUid = platformTemplateView.getTemplateId();
            } else {
                PlatformTemplate platformTemplate = editTemplate(platformTemplateView);//修改模板信息
                platformPageConfigService.updatePlatformPageConfig(platformTemplateView);//修改模板配置页面信息
                infoTemplate.setTemplateUid(platformTemplate.getTemplateId());
                platformInfoTemplateService.insertPlatformInfoTemplate(infoTemplate, platformTemplateView);
                templateUid = platformTemplateView.getTemplateId();
            }

        } else {
            if (ObjectUtil.isEmpty(platformTemplateView.getTemplateId())) throw new PlatformException("您不是管理员,无法添加模板！");
            editTemplate(platformTemplateView);//修改模板信息
            platformPageConfigService.updatePlatformPageConfig(platformTemplateView);// 修改模板配置页面信息
            templateUid = platformTemplateView.getTemplateId();
        }
        if (PublishStatusEnum.publish.getKey().equals(platformTemplateView.getPublishStatus())) {
            if (!platformTemplateView.getIsAdmin()) {// 不是管理员在发布的时候保存一份草稿
                //updateStatus(platformTemplateView.getPlatformUid(), platformTemplateView.getTemplateId(), PublishStatusEnum.revisions.getKey());
                Long oldUid = platformTemplateView.getTemplateId();
                PlatformTemplate byId = this.getById(oldUid);
                draft(oldUid, byId.getSourceTemplateId());
                platformTemplateView.setSourceTemplateId(byId.getSourceTemplateId());
                platformTemplateView.setTemplateId(null);
                platformTemplateView.setPublishStatus(PublishStatusEnum.draft.getKey());
                platformTemplateView.setHomePageImage(byId.getHomePageImage());
                platformTemplateView.setClassPageImage(byId.getClassPageImage());
                platformTemplateView.setPersonCenterImage(byId.getPersonCenterImage());
                platformTemplateView.setShopCartImage(byId.getShopCartImage());
                platformTemplateView.setShopDetailImage(byId.getShopDetailImage());
                PlatformTemplate platformTemplate = saveTemplate(platformTemplateView);
                platformTemplateView.setTemplateId(oldUid);
                platformTemplateView.setIsCopy("1");
                platformTemplateView.setPublishStatus(PublishStatusEnum.publish.getKey());
                platformPageConfigService.copyPlatformPageConfig(platformTemplateView, platformTemplate.getTemplateId());
                infoTemplate.setTemplateUid(platformTemplate.getTemplateId());
                platformInfoTemplateService.insertPlatformInfoTemplate(infoTemplate, platformTemplateView);
            }
            redisService.deleteObject(CachePrefix.TEMPLATE_HOME_PAGE + SecurityUtils.getPlatformId());
            redisService.deleteObject(CachePrefix.TEMPLATE_CLASS_PAGE + SecurityUtils.getPlatformId());
            redisService.deleteObject(CachePrefix.TEMPLATE_MY_PAGE + SecurityUtils.getPlatformId());
            redisService.deleteObject(CachePrefix.PLATFORM_GENERAL_SETTINGS + SecurityUtils.getPlatformId());
            redisService.deleteObject(CachePrefix.TEMPLATE_MALL_PAGE + SecurityUtils.getPlatformId());
        }
        return templateUid;
    }

    @Override
    public boolean updatePlatformTemplates(PlatformTemplate platformTemplate) {
        platformTemplate.setUpdateTime(DateUtil.date());
        platformTemplate.setUpdateUid(SecurityUtils.getUserId());
        platformTemplate.setUpdateName(SecurityUtils.getUsername());
        platformTemplate.setPublishStatus(PublishStatusEnum.draft.getKey());
        this.updateById((platformTemplate));
        platformPageConfigService.removeComponent(platformTemplate.getTemplateId(), PublishStatusEnum.draft.getKey(), null, null, null);
        return true;
    }


    @Override
    public boolean updatePlatformTemplate(PlatformTemplate platformTemplate) {
        platformTemplate.setUpdateTime(DateUtil.date());
        platformTemplate.setUpdateUid(SecurityUtils.getUserId());
        platformTemplate.setUpdateName(SecurityUtils.getUsername());
        return this.updateById((platformTemplate));
    }

    /**
     * 使用模板
     *
     * @param templateUid
     * @return
     */
    @Override
    public PlatformTemplateView use(Long templateUid) {
        // 用于店铺管理员看到模板的效果
        UseTypeVo useTypeVo = new UseTypeVo("0");
        if (ObjectUtil.isEmpty(templateUid)) throw new BaseException("参数异常");
        PlatformInfoTemplate infoTemplate = new PlatformInfoTemplate();
        infoTemplate.setPlatformUid(SecurityUtils.getPlatformId());
        infoTemplate.setTemplateUid(templateUid);
        PlatformTemplateView view = new PlatformTemplateView();
        view.setUserId(SecurityUtils.getUserId());
        view.setUserName(SecurityUtils.getUsername());
        view.setTime(DateUtil.date());
        if (!SecurityUtils.isAdmin(view.getUserId())) {
            //去判断是否是管理员的模板,如果不是则抛出异常
            PlatformTemplate platformTemplateAdmin = platformTemplateMapper.checkAdminTemplate(templateUid);
            if (ObjectUtil.isEmpty(platformTemplateAdmin)) {//为空说明是使用自己的模板
                // 判断是否是自己模板
                HashMap map = new HashMap();
                map.put("platformUid", SecurityUtils.getPlatformId());
                map.put("templateUid", templateUid);
                PlatformTemplate platformTemplate = platformTemplateMapper.getTemplateDritThree(map);
                if (ObjectUtil.isEmpty(platformTemplate)) throw new BaseException("模板不存在！");
                BeanUtils.copyProperties(platformTemplate, view);
                useTypeVo.setTemplateUid(view.getSourceTemplateId());
            } else {// 说明是使用的超级管理员的模板
                //根据这个去查询自己最新的草稿模板
                HashMap map = new HashMap();
                map.put("sourceTemplateId", templateUid);
                map.put("platformUid", SecurityUtils.getPlatformId());
                List<Long> templateDrit = platformTemplateMapper.getTemplateDrit(map);
                if (ObjectUtil.isEmpty(templateDrit)) {//判断是否为空,如果为空,就添加一个
                    PlatformTemplate byId = this.getById(templateUid);
                    if (ObjectUtil.isEmpty(byId)) throw new BaseException("模板不存在！");
                    BeanUtils.copyProperties(byId, view);
                    String status = view.getPublishStatus();
                    view.setPlatformUid(SecurityUtils.getPlatformId());
                    view.setTemplateId(null);
                    view.setSourceTemplateId(templateUid);
                    view.setPublishStatus(PublishStatusEnum.draft.getKey());
                    PlatformTemplate platformTemplate = saveTemplate(view);
                    infoTemplate.setTemplateUid(platformTemplate.getTemplateId());
                    view.setTemplateId(templateUid);
                    view.setPublishStatus(status);
                    platformPageConfigService.copyPlatformPageConfig(view, platformTemplate.getTemplateId());
                    view.setPublishStatus(PublishStatusEnum.draft.getKey());
                    view.setTemplateId(platformTemplate.getTemplateId());
                    platformInfoTemplateService.insertPlatformInfoTemplate(infoTemplate, view);
                } else {
                    PlatformTemplate byId = this.getById(templateDrit.get(0));
                    if (ObjectUtil.isEmpty(byId)) throw new BaseException("模板不存在！");
                    BeanUtils.copyProperties(byId, view);
                }
                useTypeVo.setTemplateUid(templateUid);
            }

        } else {
            PlatformTemplate byId = this.getById(templateUid);
            if (ObjectUtil.isEmpty(byId)) throw new BaseException("不属于超级管理员的模板！");
            BeanUtils.copyProperties(byId, view);
        }


        PlatformPageConfig pageConfig = new PlatformPageConfig();
        pageConfig.setTemplateId(view.getTemplateId());
        // 获取数据
        view.setPage(platformPageConfigService.getPlatformPageVoList(pageConfig, infoTemplate.getPlatformUid(), useTypeVo));
        view.setIsAdmin(SecurityUtils.isAdmin(view.getUserId()));
        // 获取店铺名称
        view.setPlatformName("优销云");
        PlatformInfo info = iPlatformInfoService.getById(SecurityUtils.getPlatformId());
        if (ObjectUtil.isNotEmpty(info)) {
            view.setPlatformName(info.getPlatformName());
        }
        return view;
    }


    /**
     * 添加模板信息-
     *
     * @param view
     * @return
     */
    private PlatformTemplate saveTemplate(PlatformTemplateView view) {
        PlatformTemplate platformTemplate = new PlatformTemplate();
        BeanUtils.copyProperties(view, platformTemplate);
        platformTemplate.setCreateUid(view.getUserId());
        platformTemplate.setCreateName(view.getUserName());
        platformTemplate.setCreateTime(view.getTime());
        platformTemplate.setDataStatus(DataStatus.valid.getKey());
        platformTemplate.setUpdateUid(view.getUserId());
        platformTemplate.setUpdateName(view.getUserName());
        platformTemplate.setUpdateTime(view.getTime());
        boolean save = this.save(platformTemplate);
        if (!save) throw new PlatformException("添加模板失败,请稍后再试！");
        return platformTemplate;
    }

    /**
     * 修改模板信息
     *
     * @param view
     * @return
     */
    private PlatformTemplate editTemplate(PlatformTemplateView view) {
        PlatformTemplate platformTemplate = new PlatformTemplate();
        BeanUtils.copyProperties(view, platformTemplate);
        platformTemplate.setUpdateUid(view.getUserId());
        platformTemplate.setUpdateName(view.getUserName());
        platformTemplate.setUpdateTime(view.getTime());
        platformTemplate.setDataStatus(DataStatus.valid.getKey());
        this.updateById(platformTemplate);
        return platformTemplate;
    }

    /**
     * 修改状态
     *
     * @param platformUid
     * @param dataStatus
     */
    private void updateStatus(Long platformUid, Long templateUid, String dataStatus) {
        //platformInfoTemplateService.queryList();
        // 查询模板id
        PlatformInfoTemplate infoTemplate = new PlatformInfoTemplate();
        infoTemplate.setPlatformUid(platformUid);
        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateService.queryListS(infoTemplate);
        if (ObjectUtil.isNotEmpty(platformInfoTemplates)) {
            List<Long> aLongs = new ArrayList<>();
            platformInfoTemplates.forEach(platformInfoTemplate -> {
                aLongs.add(platformInfoTemplate.getTemplateUid());
            });
            UpdateWrapper<PlatformTemplate> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("TEMPLATE_ID", aLongs);
            updateWrapper.ne("TEMPLATE_ID", templateUid);
            updateWrapper.eq("PUBLISH_STATUS", PublishStatusEnum.publish.getKey());
            updateWrapper.set("PUBLISH_STATUS", dataStatus);
            updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
            updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
            updateWrapper.set("UPDATE_TIME", DateUtil.date());
            this.update(updateWrapper);
            platformInfoTemplateService.removePlatformInfoTemplateByIds(platformUid, aLongs, templateUid);

            platformPageConfigService.removeComponent(null, dataStatus, platformUid, templateUid, aLongs);
        }

    }

    /**
     * 查询草稿版本
     *
     * @param templateId
     * @return
     */
    private PlatformTemplate getCaoG(Long templateId) {
        PlatformInfoTemplate infoTemplate = new PlatformInfoTemplate();
        infoTemplate.setPlatformUid(SecurityUtils.getPlatformId());
        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateService.queryList(infoTemplate);
        if (ObjectUtil.isEmpty(platformInfoTemplates)) return null;
        List<Long> aLongs = new ArrayList<>();
        platformInfoTemplates.forEach(platformInfoTemplate -> {
            aLongs.add(platformInfoTemplate.getTemplateUid());
        });
        QueryWrapper<PlatformTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("TEMPLATE_ID", aLongs);
        queryWrapper.eq("SOURCE_TEMPLATE_ID", templateId);
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        queryWrapper.eq("PUBLISH_STATUS", PublishStatusEnum.draft.getKey());
        return platformTemplateMapper.selectOne(queryWrapper);
    }

    /**
     * 将上一个发布的设置为草稿
     */
    private void draft(Long templateUid, Long soureUid) {
        Date time = DateUtil.date();
        Long userUid = SecurityUtils.getUserId();
        //Long onTine = platformPageConfigService.getOnTine(templateUid);
        HashMap map = new HashMap();
        map.put("platformUid", SecurityUtils.getPlatformId());
        map.put("sourceTemplateId", soureUid);
        map.put("templateUid", templateUid);
        // 拿到来源id模板
        List<Long> templateDrit = platformTemplateMapper.getTemplateDrit(map);
        //查询有效的模板
        List<Long> list = platformTemplateMapper.getTemplateDritTwo(map);
        if (ObjectUtil.isNotEmpty(list)) {
            list.forEach(info -> {
                templateDrit.add(info);
            });
        }
        if (ObjectUtil.isNotEmpty(templateDrit)) {
            UpdateWrapper<PlatformTemplate> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("TEMPLATE_ID", templateDrit);
            updateWrapper.ne("TEMPLATE_ID", templateUid);
            updateWrapper.set("PUBLISH_STATUS", PublishStatusEnum.revisions.getKey());
            updateWrapper.set("update_time", time);
            updateWrapper.set("update_uid", userUid);
            this.update(updateWrapper);
            platformPageConfigService.removeComponent(null, PublishStatusEnum.revisions.getKey(), SecurityUtils.getPlatformId(), templateUid, templateDrit);
            platformInfoTemplateService.removePlatformInfoTemplateByIds(SecurityUtils.getPlatformId(), templateDrit, templateUid);
        }


    }

}
